﻿// 设置权限
var setAuthorize;
// 获取表单数据
var getFormData;
// 设置表单数据
var setFormData;
// 验证数据是否填写完整
var validForm;
var validBusinessData;
// 保存数据
var save;
var saveDraft;
//不可编辑
var setFormDisabled;

var isUpdate = false;

var bootstrap = function ($, learun) {
    "use strict";

    var userinfo;

    // 设置权限
    setAuthorize = function (data, isLook) {
        userinfo = learun.clientdata.get(['userinfo']);
        $("#createUsername").val(userinfo.user.realName);
        //报销人
        $("#userid").lrformselect({
            layerUrl: config.webportal_server + 'pages/system/user/selectOnlyForm.html?companyId=' + userinfo.user.companyId + '&access_token=' + config.getToken().access_token,
            layerUrlW: 400,
            layerUrlH: 300,
            dataUrl: config.base_server() + 'users/getNames?access_token=' + config.getToken().access_token,
        }).lrformselectSet(userinfo.user.id);
        //随行人员
        $("#followUserids").lrformselect({
            layerUrl: config.webportal_server + 'pages/system/user/selectForm.html?access_token=' + config.getToken().access_token,
            layerUrlW: 1024,
            layerUrlH: 768,
            dataUrl: config.base_server() + 'users/getNames?access_token=' + config.getToken().access_token,
        });
        // //目的地
        // $("#destination").lrformselect({
        //     layerUrl: config.webportal_server + 'pages/system/area/selectOnlyForm.html?access_token=' + config.getToken().access_token,
        //     layerUrlW: 600,
        //     layerUrlH: 400,
        //     dataUrl: config.base_server() + 'areas/getName?access_token=' + config.getToken().access_token
        // });
        //车牌选择
        $("#licensePlateNumber").lrselect({
            value: 'text',
            text: 'text',
            title: 'text',
            placeholder: '请选择车辆',
            // 是否允许搜索
            allowSearch: true,
            select: function (item) {
                if (!!item && item.value != '') {
                    $.ajax({
                        url: config.base_server() + 'cars/findById?id=' + item.value + '&access_token=' + config.getToken().access_token,
                        type: "post",
                        dataType: "json",
                        data: {},
                        async: false,
                        success: function (result) {
                            if (result != null) {
                                //if (!$("#driverName").val() && !$("#startMileage").val()) {
                                $("#driverName").val(result.data.driverName);
                                $("#startMileage").val(result.data.mileage);
                                //}
                                $("#driverName").lrRemoveValidMessage();
                                $("#startMileage").lrRemoveValidMessage();
                            }
                        }
                    });
                }
            }
        });

        $('#gridTable').css("width", $('.lr-form-wrap').width() - 60);

        //禁用输入框自动填充
        $("input[type='text']").each(function (index) {
            this.autocomplete = "off";
        });
    };
    //设置表单不可编辑
    setFormDisabled = function (wfType, upload) {
        if (!!wfType && wfType == 100) {
            $('#form').find('input,textarea,select').attr('disabled', 'disabled');
            $('#driverName').removeAttr("disabled");
            $('#startMileage').removeAttr("disabled")
            $("#userid").unbind();

            $("#followUserids").unbind();
            $("#destination").unbind();
        }
    };
    // 获取表单数据
    getFormData = function () {
        var formData = $('#form').lrGetFormData();

        var postData = formData;
        postData.id = parent.processId;
        return postData;
    };
    // 设置表单数据
    setFormData = function (processId, wfType, checkPermission, upload) {
        if (checkPermission == true) { //审核时只能看见可用车辆
            $("#licensePlateNumber").lrselectRefresh({
                url: config.base_server() + 'cars/tree?status=1&access_token=' + config.getToken().access_token,
                param: {}
            });
        } else {
            $("#licensePlateNumber").lrselectRefresh({
                url: config.base_server() + 'cars/tree?access_token=' + config.getToken().access_token,
                param: {}
            });
        }

        if (processId) {
            $('#files').lrUploader({isView: false});
            $.lrSetForm(config.base_server() + 'carapply1s/findByProcessId?processId=' + processId, function (data) {
                console.log(data);
                if (data != null) {
                    isUpdate = true;
                    $('#form').lrSetFormData(data);
                    $("#processCode").html(data.processCode);
                    var destinationStr = data.destinationStr.split('/');
                    $("#destination").citypicker({
                        province: destinationStr[0],
                        city: !!destinationStr[1] ? destinationStr[1] : "",
                        district: !!destinationStr[2] ? destinationStr[2] : ""
                    });
                    $("#vehicle").find('input[type="checkbox"]').each(function (r) {
                        if (data.vehicle.indexOf($(this).val()) >= 0) {
                            $(this).prop("checked", true);
                        }
                    });
                    setTimeout(function () {
                            $("#driverName").val(data.driverName);
                            $("#startMileage").val(data.startMileage);
                        },
                        300
                    )

                } else {
                    $("#destination").citypicker();
                }
                setPbtn(wfType, checkPermission);
            });
        } else {
            setPbtn(wfType, checkPermission);
        }
        setFormDisabled(wfType, upload);
    };
    // 验证数据是否填写完整
    validForm = function (verifyType) {
        if (verifyType == '2') {
            $('#licensePlateNumber').removeAttr("isvalid");
            $('#driverName').removeAttr("isvalid");
            $('#startMileage').removeAttr("isvalid");
        }
        if (!$('#form').lrValidform()) {
            return false;
        }

        return true;
    };
    validBusinessData = function () {
        return true;
    }
    // 保存调用函数
    save = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();

        var postData = formData;
        postData.id = parent.processId;

        $.lrSaveForm(config.base_server() + 'carapply1s/saveOrUpdate?keyValue=' + keyValue + '&force=' + force + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };

    // 保存调用函数
    saveDraft = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();
        formData.vehicle = "";
        $("#vehicle").find('input[type="checkbox"]').each(function (r) {
            if ($(this).prop("checked")) {
                if (formData.vehicle == "") {
                    formData.vehicle += $(this).val();
                } else {
                    formData.vehicle += ("," + $(this).val());
                }

            }
        });


        var postData = formData;
        postData.id = parent.processId;

        $.lrSaveForm(config.base_server() + 'carapply1s/saveOrUpdate?keyValue=' + keyValue + '&force=' + force + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };


    // 设置父级窗口的按钮
    function setPbtn(wfType, checkPermission) {
        if (!!wfType && wfType == 100 && checkPermission == true) { //审核
            // $('#lr-layout-panel-right', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#flow-projectinfo', window.parent.document).hide();
            $('#div_car').show();
        } else if (!!wfType && wfType == 100 && checkPermission == false) { //查看
            $('#lr-layout-panel-right', window.parent.document).empty();
            $('#div_car').show();

             $("#licensePlateNumber").unbind();
            $('#form').find('input,textarea,select').attr('disabled', 'disabled');
        } else {
            $('#div_car').hide();
            $('#licensePlateNumber').removeAttr("isvalid");
            $('#driverName').removeAttr("isvalid");
            $('#startMileage').removeAttr("isvalid");

            $('#release', window.parent.document).text('提交申请');
            // $('.lr-form-item-title', window.parent.document).text('备注');
            //$('#savedraft', window.parent.document).hide();
            $('#verifyBill', window.parent.document).hide();
            $('#flow-projectinfo', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#sign', window.parent.document).hide();
            $('#flow-title input', window.parent.document).val("用车申请");
            $('#flow-title input', window.parent.document).attr("readonly", "readonly");
            //$('#print', window.parent.document).hide();
        }
    }
}
